package com.ruoyi.web.controller.consumption;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SnowflakeIdUtils;
import com.ruoyi.consumption.dto.ConsumptionDto;
import com.ruoyi.control.domain.Electricity;
import com.ruoyi.control.service.IElectricityService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.consumption.domain.Consumption;
import com.ruoyi.consumption.service.IConsumptionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 能耗Controller
 * 
 * @author hcc
 * @date 2024-11-25
 */
@RestController
@RequestMapping("/control/consumption")
public class ConsumptionController extends BaseController
{
    @Autowired
    private IConsumptionService consumptionService;

    @Autowired
    private IElectricityService electricityService;
    //TODO
    //@Scheduled(cron = "0 0 0/4 * * ?")
    public void insertConsumption(){
        Consumption consumption = new Consumption();
        Electricity dm_1000_uab = electricityService.selectElectricityByTagName("dm_1000_Uab");
        consumption.setAh1("dm_1000");
        consumption.setAh1Analog1(dm_1000_uab.getValue());
        Electricity dm_1000_Ia = electricityService.selectElectricityByTagName("dm_1000_Ia");
        consumption.setAh1Analog4(dm_1000_Ia.getValue());
        Electricity dm_1010_Uab = electricityService.selectElectricityByTagName("dm_1010_Uab");
        consumption.setAh32("dm_1010");
        consumption.setAh32Analog1(dm_1010_Uab.getValue());
        Electricity dm_1010_Ia = electricityService.selectElectricityByTagName("dm_1010_Ia");
        consumption.setAh32Analog4(dm_1010_Ia.getValue());

        consumption.setDataTime(new Date());
        consumption.setConsumptionId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        consumptionService.insertConsumption(consumption);
    }

    /**
     * 查询能耗列表
     */
    @PreAuthorize("@ss.hasPermi('control:consumption:list')")
    @GetMapping("/list")
    public TableDataInfo list(Consumption consumption)
    {
        startPage();
        List<Consumption> list = consumptionService.selectConsumptionList(consumption);
        return getDataTable(list);
    }

    /**
     * 查询能耗曲线
     */
    //@PreAuthorize("@ss.hasPermi('control:consumption:list')")
    @GetMapping("/getData")
    public ConsumptionDto getData()
    {
        ConsumptionDto consumptionDto = consumptionService.getData();
        return consumptionDto;
    }

    /**
     * 导出能耗列表
     */
    @PreAuthorize("@ss.hasPermi('control:consumption:export')")
    @Log(title = "能耗", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Consumption consumption)
    {
        List<Consumption> list = consumptionService.selectConsumptionList(consumption);
        ExcelUtil<Consumption> util = new ExcelUtil<Consumption>(Consumption.class);
        util.exportExcel(response, list, "能耗数据");
    }

    /**
     * 获取能耗详细信息
     */
    @PreAuthorize("@ss.hasPermi('control:consumption:query')")
    @GetMapping(value = "/{consumptionId}")
    public AjaxResult getInfo(@PathVariable("consumptionId") String consumptionId)
    {
        return success(consumptionService.selectConsumptionByConsumptionId(consumptionId));
    }

    /**
     * 新增能耗
     */
    @PreAuthorize("@ss.hasPermi('control:consumption:add')")
    @Log(title = "能耗", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Consumption consumption)
    {
        return toAjax(consumptionService.insertConsumption(consumption));
    }

    /**
     * 修改能耗
     */
    @PreAuthorize("@ss.hasPermi('control:consumption:edit')")
    @Log(title = "能耗", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Consumption consumption)
    {
        return toAjax(consumptionService.updateConsumption(consumption));
    }

    /**
     * 删除能耗
     */
    @PreAuthorize("@ss.hasPermi('control:consumption:remove')")
    @Log(title = "能耗", businessType = BusinessType.DELETE)
	@DeleteMapping("/{consumptionIds}")
    public AjaxResult remove(@PathVariable String[] consumptionIds)
    {
        return toAjax(consumptionService.deleteConsumptionByConsumptionIds(consumptionIds));
    }
}
